<?php
	function f210($sql){

		$dataHandle = new DataHandle();
		$dataHandle->setRequest("210");
		
		$dataType = getValue($_GET['dataType']);
		
		if ($dataType == "json"){
			$format = array(
				"item" => "numb6",
			);
			$dataHandle->setFormat($format);
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "lin_produced","start_time");
				$query = getQuery($startTime, $endTime, $filter);
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else{
			$temp = array();
			$temp['error'] = "210: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);
	}

	function getQuery(&$startTime, &$endTime, &$filter){
		
		$queryString = 
			"
				select

				lin_produced.line_id as line,
				lin_products.speed as speed,
				sum(lin_produced.minutes) time,
				sum(lin_produced.pouches_total) pouchesTotal,
				sum(lin_produced.pouches) pouchesActual,
				lin_produced.product_id as item,
				lin_products.description,

				case 
					when	
						cast(
							avg(
								round(
									(
										100 
										* lin_Produced.pouches
									)
									/	(
											lin_Produced.speed 
											* dbo.lin_calc_working_time(
												lin_Produced.start_time, lin_Produced.end_time,lin_Products.line_id
											)
									)
								, 1)
							)
						AS numeric(8, 1)) 
						> 100
					then 100
					else
						cast(
							avg(
								round(
									(
										100 
										* lin_Produced.pouches
									)
									/	(
											lin_Produced.speed 
											* dbo.lin_calc_working_time(
												lin_Produced.start_time, lin_Produced.end_time,lin_Products.line_id
											)
									)
								, 1)
							)
						AS numeric(8, 1)) 
				end as productionEfficiency

				--lin_produced.notes as comment

				FROM lin_produced
				INNER JOIN lin_products ON lin_produced.product_id=lin_products.product_id";

				$queryString = $queryString.$filter;

				$queryString = $queryString."
					group by lin_produced.line_id, lin_products.speed, lin_produced.product_id, lin_products.description--,lin_produced.notes
					order by lin_produced.line_id;";

				return $queryString;
	}
?>